<?php

namespace RedMVC;
 
/**
 * Session class
 *  
 * @category    RedMVC
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class Session{

    const NAMESPACE_DEFAULT  = 'RedMVC';

    protected static $_instance = null;
    protected static $_currentNamespace;
    
    private function __construct(){
    
    }
       
    public static function getInstance($namespace = self::NAMESPACE_DEFAULT){
        if(null === self::$_instance){
            self::$_instance = new self();
            $config = Registry::get('RedMVC_Config');
            if(isset($config->sessionSavePath) && $config->sessionSavePath !== false){
                session_save_path($config->sessionSavePath);
            }
            if(isset($config->sessionName) && $config->sessionName !== false){
                session_name($config->sessionName);
            }
            session_start();
        }
        self::$_currentNamespace = $namespace;
        return self::$_instance;
    }
    
    public static function set($key, $value, $namespace = self::NAMESPACE_DEFAULT){
        self::getInstance($namespace);
        $_SESSION[$namespace][$key] = $value;
    }
    
    public static function get($key, $namespace = self::NAMESPACE_DEFAULT){
        self::getInstance($namespace);
        if(null === $key && isset($_SESSION[$namespace])){
            return $_SESSION[$namespace];
        }
        else if(isset($_SESSION[$namespace][$key])){
            return $_SESSION[$namespace][$key];
        }
        return null;
    }
    
    public static function exists(){
        if(ini_get('session.use_cookies') == '1' && isset($_COOKIE[session_name()])){
            return true;
        } 
        else if(!empty($_REQUEST[session_name()])){
            return true;
        }
        return false;
    }
    
    public static function destroy(){
        $_SESSION = array();
        if(ini_get("session.use_cookies")){
            $params = session_get_cookie_params();
            setcookie(session_name(), '', time() - 42000, $params["path"],
                $params["domain"], $params["secure"], $params["httponly"]
            );
        }
        session_destroy();
    }
    
    public static function id(){
        self::getInstance();
        return session_id();
    }
    
    public function __set($key, $value){
        $_SESSION[self::$_currentNamespace][$key] = $value;
    }
    
    public function __get($key){
        if(isset($_SESSION[self::$_currentNamespace][$key])){
            return $_SESSION[self::$_currentNamespace][$key];
        }
        return null;
    }
    
    public function __isset($key){
        return isset($_SESSION[self::$_currentNamespace][$key]);
    }
    
    public function __unset($key){
        unset($_SESSION[self::$_currentNamespace][$key]);
    }
    
    public function namespaceUnset(){
        unset($_SESSION[self::$_currentNamespace]);
    }
    
    public function namespaceExists(){
        return isset($_SESSION[self::$_currentNamespace]);
    }
 
}